//
//  NSDate+SYExtension.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/20.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "NSDate+SYExtension.h"

@implementation NSDate (SYExtension)

+(NSString *)getNowDateStringYMD
{
    NSDate *date = [NSDate date];
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:date];
}
+(NSString *)getPreDateStringDateString:(NSString *)dateString
{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *fromeDate = [dateFormatter dateFromString:dateString];
    NSTimeInterval time = 1 * 24 * 60 * 60;//一年的秒数
    NSDate * lastDate = [fromeDate dateByAddingTimeInterval:-time];
    NSString *lastDateS = [[dateFormatter stringFromDate:lastDate] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return lastDateS;
}
+(NSArray *)getFixedTimeDifferenceFieldFixDay:(NSInteger)fixDay FromDateString:(NSString *)fromDateString
{
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *fromeDate = [dateFormatter dateFromString:fromDateString];
    NSTimeInterval time = fixDay * 24 * 60 * 60;//一年的秒数
    NSDate * lastDate = [fromeDate dateByAddingTimeInterval:-time];
    NSString *lastDateString = [dateFormatter stringFromDate:lastDate];
    NSString *fromDateS = [[NSString stringWithFormat:@"%@ %@",fromDateString , @"00:00:00"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *lastDateS = [[NSString stringWithFormat:@"%@ %@",lastDateString , @"00:00:00"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return @[lastDateS , fromDateS];
    
}


+(BOOL)stockTradingTimeJudgment
{
    //节假日判断
    if (![[NSDate date] countryHolidayJudgment]) {
        return NO;
    }
    //星期天
    NSDate *date = [NSDate date];
    NSCalendar * myCalendar = [NSCalendar currentCalendar];
    myCalendar.timeZone = [NSTimeZone systemTimeZone];
    NSInteger week = [[myCalendar components:NSCalendarUnitWeekday fromDate:date] weekday];
    if (week == 1 || week == 7) {//周六周日
        return NO;
    }
    //时间节点
    if (![[NSDate date] timeJudgment]) {
        return NO;
    }
    return YES;
}


-(BOOL)timeJudgment
{
    NSLocale * locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
    //获取当前年月日的时间字段
    NSDate *yearDate = [NSDate date];
    NSDateFormatter *yearFormate = [[NSDateFormatter alloc] init];
    [yearFormate setLocale:locale];
    [yearFormate setDateFormat:@"yyyy:MM:dd"];
    NSString *yearString = [yearFormate stringFromDate:yearDate];
    //上午开始时间和结束时间 ， 下午开始时间和结束时间
    NSString *shangwuStartStr = [NSString stringWithFormat:@"%@ %@",yearString , @"09:30:00"];
    NSString *shangwuEndStr = [NSString stringWithFormat:@"%@ %@",yearString , @"11:30:00"];
    
    NSString *xiawuStartStr = [NSString stringWithFormat:@"%@ %@",yearString , @"13:00:00"];
    NSString *xiawuEndStr = [NSString stringWithFormat:@"%@ %@",yearString , @"14:50:00"];
    
    //获取当前时间
    NSDate *today = [NSDate date];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy:MM:dd HH:mm:ss"];//HH 使用 24 小时制；hh 12小时制
    [formate setLocale:locale];
    //当前时间
    NSString *nowStr = [formate stringFromDate:today];
    //将全部时间字段转换成转换成NSDate类型，日期默认
    NSDate *nowDate = [formate dateFromString:nowStr];
    NSDate *shangwuStartDate = [formate dateFromString:shangwuStartStr];
    NSDate *shangwuEndDate = [formate dateFromString:shangwuEndStr];
    NSDate *xiawuStartDate = [formate dateFromString:xiawuStartStr];
    NSDate *xiawuEndDate = [formate dateFromString:xiawuEndStr];
    
    if (([nowDate compare:shangwuStartDate] == NSOrderedDescending && [nowDate compare:shangwuEndDate] == NSOrderedAscending) || ([nowDate compare:xiawuStartDate] == NSOrderedDescending && [nowDate compare:xiawuEndDate] == NSOrderedAscending)) {
        return YES;
    }
    return NO;
}
//判断是不是法定节假日
-(BOOL)countryHolidayJudgment
{
    //加载本地数据
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"holidays.plist" ofType:nil]];
    NSArray *holidays = dict[@"holidays"];
    NSLocale * locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
    //获取当前年月日的时间字段
    NSDate *yearDate = [NSDate date];
    NSDateFormatter *yearFormate = [[NSDateFormatter alloc] init];
    [yearFormate setLocale:locale];
    [yearFormate setDateFormat:@"yyyy:MM:dd"];
    NSString *yearString = [yearFormate stringFromDate:yearDate];
    for (NSString *holiday in holidays) {
        if ([yearString isEqualToString:holiday]) {
            return NO;
        }
    }
    return YES;
}

+(NSInteger)hadStartedTimeWithBeginTime:(NSString *)beginTime
{
    //转成需要的时间格式
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    formate1.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date1 = [formate1 dateFromString:beginTime];
    NSString *begin1 = [formate1 stringFromDate:date1];
    
    DDLog(@"begin1 = %@" , begin1)
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *beginTimeStr = begin1;
    NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];
    NSDate *startDate = [dateFomatter dateFromString:beginTimeStr];
    NSDate *expireDate = [dateFomatter dateFromString:nowDateStr];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateCom = [calendar components:unit fromDate:startDate toDate:expireDate options:0];
    DDLog(@"dateCom.day = %zd" , dateCom.day)
    return dateCom.day;
}


+(NSInteger)computationalTimeWithBeginTime:(NSString *)beginTime andEndTime:(NSString *)endTime
{
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    formate1.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date1 = [formate1 dateFromString:beginTime];
    NSString *begin1 = [formate1 stringFromDate:date1];
    
    NSDate *nowDate = [formate1 dateFromString:endTime];
    NSString *end1 = [formate1 stringFromDate:nowDate];
    
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *beginTimeStr = begin1;
    NSString *nowDateStr = end1;
    
    NSDate *startDate = [dateFomatter dateFromString:beginTimeStr];
    NSDate *expireDate = [dateFomatter dateFromString:nowDateStr];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    //    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    //    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    NSCalendarUnit unit = NSCalendarUnitDay;
    NSDateComponents *dateCom = [calendar components:unit fromDate:startDate toDate:expireDate options:0];
    DDLog(@"dateCom.day = %zd" , dateCom.day)
    return dateCom.day + 1;
}

+(BOOL)isTodayWithTime:(NSString *)timeString
{
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    dateFomatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [dateFomatter dateFromString:timeString];
    return [date isToday];
}

- (BOOL)isToday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);
}

-(NSString *)weekDay
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitWeekday;
    comps = [calendar components:unitFlags fromDate:self];
    NSInteger week = [comps weekday] ;
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = kCFNumberFormatterRoundHalfDown; //输出一，二，三，四
    
    NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:week == 1 ? @(7).intValue :@(week - 1).intValue]];
    //    NSString *string = [formatter stringFromNumber:];
    
    NSInteger w =   week == 1 ? @(7).integerValue :@(week - 1).integerValue;
    
    switch (w) {
        case 1:
            string = @"一";
            break;
        case 2:
            string = @"二";
            break;
        case 3:
            string = @"三";
            break;
        case 4:
            string = @"四";
            break;
        case 5:
            string = @"五";
            break;
        case 6:
            string = @"六";
            break;
        case 7:
            string = @"日";
            break;
        default:
            break;
    }
    
    
    return [NSString stringWithFormat:@"星期%@",string];
}

-(NSString *)monthDay
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitWeekday;
    comps = [calendar components:unitFlags fromDate:self];
    NSInteger day = [comps day];
    return [NSString stringWithFormat:@"%zd",day];
}


@end
